package com.nerve.cloudoffice.common;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * hibernate的模版<br>
 * @author 集成显卡
 *	@time 2010.12.3
 */
public class HibernateUnit {
	
	public static SessionFactory sessionFactory;
	
	static{
		sessionFactory=new Configuration().configure().buildSessionFactory();
		//sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	
	/**
	 * 返回一个session
	 * @return
	 */
	public static Session getSession(){
		return sessionFactory.openSession();
		//return sessionFactory.getCurrentSession();
	}
	
	/**
	 * 关闭session
	 * @param session
	 */
	public static void freeSession(Session session){
		if(session!=null&&session.isOpen()){
			session.beginTransaction().commit();
			session.close();
		}
	}
}
